﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenExpressApp
{
    public class ReadOnlyRegister : IReadOnlyRegister
    {
        private IList<IReadOnlyChecker> allViewReadOnlyCheck = new List<IReadOnlyChecker>();
        private Dictionary<Type, IList<IReadOnlyChecker>> readOnlyCheckDic = new Dictionary<Type, IList<IReadOnlyChecker>>();

        public void ReadOnlyCheckRegister(Type view, IReadOnlyChecker readOnlyCheck)
        {
            IList<IReadOnlyChecker> readOnlyList;

            if (!readOnlyCheckDic.ContainsKey(view))
            {
                readOnlyList = new List<IReadOnlyChecker>();

                readOnlyCheckDic.Add(view, readOnlyList);
            }
            else 
            {
                readOnlyCheckDic.TryGetValue(view, out readOnlyList); 
            }
            readOnlyList.Add(readOnlyCheck);               

        }

        public void ReadOnlyCheckRegister(IReadOnlyChecker readOnlyCheck)
        {
            if (!allViewReadOnlyCheck.Contains<IReadOnlyChecker>(readOnlyCheck))
                allViewReadOnlyCheck.Add(readOnlyCheck);
        }

        public IList<IReadOnlyChecker> GetReadOnlyCheckList(Type view)
        {
            IList<IReadOnlyChecker> result = new List<IReadOnlyChecker>();
            foreach (IReadOnlyChecker item in allViewReadOnlyCheck)   //添加公共的检验规则
                result.Add(item);

            IList<IReadOnlyChecker> currCheckList;
            if (readOnlyCheckDic.TryGetValue(view, out currCheckList))
            {
                foreach (IReadOnlyChecker item in currCheckList)
                    result.Add(item);
            }

            return result;         
        }

        public IList<IReadOnlyChecker> GetReadOnlyCheckList()
        {
            return allViewReadOnlyCheck;
        }
        
    }
}
